<?php
namespace addons\shopro\listener;
use app\common\model\Userchildren;
class UserRegister
{
    public function run(&$params)
    {
        $member = $params;
//        // 记录团队关系邀请人邀请人
        $model = new Userchildren();
        $to_insert = [['member_id' => $member->parent_user_id, 'level' => 1, 'children_id' => $member->id]];
//        dump($to_insert);exit();
        $model->where('children_id', $member->parent_user_id)->chunk(100000, function($items) use ($member, &$to_insert,$model) {
            foreach ($items as $item) {
                $to_insert[] = [
                    'member_id' => $item->member_id,
                    'level' => $item['level'] + 1,
                    'children_id' => $member->id,
                ];
            }

            $model->insertAll($to_insert);
            $to_insert = [];
        },'level','asc');
        if (!empty($to_insert)) {
            $model->insertAll($to_insert);
        }
    }
}

